package controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.user.UserService;

import beans.UserVO;

@Controller
public class ElegirAccionController {

	@Autowired
	UserService userService;

	@RequestMapping(value = "public/goLogin", method = RequestMethod.GET)
	public ModelAndView goLoginController() {
		ModelAndView mv = new ModelAndView();
		System.out.println("Entre al login bien");
		mv.setViewName("login");
		return mv;
	}

	@RequestMapping(value = "public/login", method = RequestMethod.GET)
	public ModelAndView loginController(@ModelAttribute UserVO userVO)
			throws Exception {

		System.out.println("Entre a la validacion del login bien");

		ModelAndView mv = new ModelAndView();

		if (this.getUserService().exist(userVO)) {
			mv.setViewName("menu");
		} else {
			mv.addObject("paramReason",
					"Error de logueo, usuario o contrasenia incorrecto.");
			mv.setViewName("errorLogin");
		}
		return mv;
	}

	@RequestMapping(value = "public/goMenu", method = RequestMethod.GET)
	public ModelAndView goMenuController() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("menu");
		return mv;
	}

	@RequestMapping(value = "public/menu", method = RequestMethod.GET)
	public ModelAndView elegirMenu(@RequestParam String submitMenu)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		if (submitMenu.equals("users")) {
			mv.setViewName("elegirAccionUser");
		}

		if (submitMenu.equals("restaurants")) {
			mv.setViewName("elegirAccionRestaurant");
		}
		return mv;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}